#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 21 14:18:12 2015

@author: azzollini
"""

from pdb import set_trace as stop
import numpy as np


from pySkySim.scenes import SkyScene
from pySkySim import losvd, sed

class Star(SkyScene):
    """ """
    
    def __init__(self,Cen,flux=0.,wref=10.,sptype='',vel=0.):
        """ """
        
        super(Star,self).__init__()
        
        self.Cen = Cen
        self.flux = flux # flux in Jy at reference wavelength, wref
        self.wref = wref # reference wavelength for flux
        self.sptype=sptype # SED keyword
        self.vel = vel

        self.get_SED(sed.StarSed(sptype))
        
        
    def __call__(self,Pos,wavel,time=0.):
        """ """
        return np.zeros_like(wavel)
        
    def __str__(self):
        """Prints Parameters of Object"""
        verbose = '\n\nI am a star\n'
        verbose += super(Star,self).__str__()
        return verbose


class Extended(SkyScene):
    """ """

    def __init__(self):
        """ """
        
        super(Extended,self).__init__()
        
        self.vel_map = None
    
    def spatial(self,Pos):
        """ """
        return None
    
    def __call__(self,Pos,wavel,time=0.):
        """ """
        #radii = self.isophotes(Pos) # retrieves radii of isophotes at each point in Pos
        #nim = self.radprof(radii) # normalized instensity map
        
        NIM = self.spatial(Pos) # Normalized Intensity Map
        v = self.vel_map(Pos)   # Velocity Map
        SIM = NIM * self.SED(wavel,v=v) 
        
        return SIM
        

    def __str__(self):
        """ """
        verbose = '\n\nI am an extended object\n'
        verbose += super(Extended,self).__str__()
        return verbose
    
    
    def get_velmap(self,velmap_obj):
        """ """
        canget = issubclass(type(velmap_obj),losvd.VeloMap)
        if not canget:
            print('object cannot acquire velmap_obj of type %s' % str(type(velmap_obj)))
            raise RuntimeError
        self.vel_map = velmap_obj
        print('WARNING: CRINGING STEP!')
        # a non-bound "object-method" is modified using another non-bound "object-method"
        self.SED.setLOSVD(self.vel_map.losvd) 
        
        

class Background(SkyScene):
    """Class for 'Sky + Observatory' Background.
    Inherits from SkyScene."""
    
    def __init__(self,temp,pupilfrac=1.,degree=0,maxgrad=0.,timevar=False,\
         coeffs=None):
        """ """
        
        super(Background,self).__init__()
        
        self.temp = temp # temperature of background (assumed BB-SED)
        self.pupilfrac = pupilfrac # scaling factor, given as area of BB
                                   # emitter, in units of MIRI pupil entrance area.
        self.degree = degree # polynomial degree
        self.timevar = timevar # is it time-dependent?
        
        if coeffs is None:
            self.getcoeffs()
        
        self.tmp_value = 7
    
    def getcoeffs(self,time=0.):
        """ """
        self.coeffs = np.zeros(self.degree+1,dtype='float64')
    
    
    def __call__(self,Pos,wavel,time=0.):
        """Evaluates Background."""
        # if background is time-dependent, obtain new polynomial coeffs.
        if self.timevar: self.getcoeffs(time) 
        
        return np.zeros_like(wavel) + self.tmp_value


    def __str__(self):
        """ """
        verbose = '\n\nI am (annoying) Background Emission\n'
        manifesto_bkg = 'T=%.2f\nP.F.=%.2f\nDeg=%i\nTimevar=%s\n'+\
         'coeffs = ('+'%.2f '*len(self.coeffs)
        verbose += manifesto_bkg % \
         ((self.temp,self.pupilfrac,self.degree,self.timevar)+tuple(self.coeffs)) + ' )'
        return verbose


# A minimal test is run when this file is run as a main program.
if __name__ == '__main__':
    print("Testing the module\n")
    # Basic code/example test
    print("Test finished.")